<template>
  <div class="">
    <van-button
      :icon="value === 1 ? 'good-job' : 'good-job-o'"
      :class="{
        liked: value === 1,
      }"
      :loading="loading"
      @click="onCollect"
    />
  </div>
</template>

<script>
import { addLike, deleteLike } from '@/api/article';
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      required: true,
    },
    acticleId: {
      type: [Number, Object, String],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    async onCollect() {
      this.loading = true;
      try {
        let status = -1;
        if (this.value === 1) {
          await deleteLike(this.acticleId);
        } else {
          await addLike(this.acticleId);
          status = 1;
        }
        this.$emit('input', status);
        this.$toast.success(this.value === 1 ? '取消点赞成功' : '点赞成功');
      } catch (err) {
        this.$toast.fail('操作失败，请重试！');
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
